package com.example.first.activities;


import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.first.R;
import com.example.first.modle.ApiResponse;
import com.example.first.modle.LoginRequest;
import com.example.first.services.ApiService;
import com.example.first.utils.RetrofitClient;

import retrofit2.Call;

public class LoginActivity extends AppCompatActivity {

    private EditText Email;
    private EditText Password;
    private Button btnLogin;
    private Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Email = findViewById(R.id.lg_email);
        Password = findViewById(R.id.lg_password);
        btnLogin = findViewById(R.id.btn_login);
        btnRegister = findViewById(R.id.btn_register);

        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = Email.getText().toString().trim();
                String password = Password.getText().toString().trim();

                LoginRequest loginRequest = new LoginRequest(email, password);


                ApiService apiService = RetrofitClient.getApiService();
                Call<ApiResponse<String>> call = apiService.login(loginRequest);
                call.enqueue(new retrofit2.Callback<ApiResponse<String>>() {
                    @Override
                    public void onResponse(Call<ApiResponse<String>> call, retrofit2.Response<ApiResponse<String>> response) {
                        if(response.isSuccessful() && response.body()!=null){
                            if(response.body().getCode()==1){

                                RetrofitClient.setToken(response.body().getData());
                                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                //回到主界面
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            }else {
                                Toast.makeText(LoginActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<ApiResponse<String>> call, Throwable t) {
                        //处理失败情况
                        t.printStackTrace();
                    }
                });
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }



    /**
     * 重写返回键,退回到主页面
     */
    @SuppressLint("MissingSuperCall")
    @Override
    public void onBackPressed() {
        //回到MainActivity
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }



}
